﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using MINERP.InfoConfig.Controller;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.InfoConfig
{
    public partial class FrmPrinterManager : FrmMDIChildFormTemplate
    {
        private PrinterController   _controller =  new PrinterController();
        private PrinterInfo         _printer = null;
        static FrmPrinterManager instance = null;
        private FrmPrinterManager()
        {
            InitializeComponent();
        }
      
        public static FrmPrinterManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmPrinterManager();
                }
                return instance;
            }
        }

        private void FrmPrinterManager_Load(object sender, EventArgs e)
        {
            comboBoxCutFlagFrontOrBehind.SelectedIndex = 0;
            //link datagrid
            LinkPrinter();
            LinkSubArea();
            LinkPaperFace();
            LinkPaperType();
            LinkPaperType1();
          
        }

        // Add Shop
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtPName.Focus();
                return;
            }
            if (CheckData())
            {
                PrinterInfo printer = new PrinterInfo();
                printer.PName = txtPName.Text;
                printer.PDPI = Convert.ToInt32(txtPDPI.Text);
                printer.PLength = Convert.ToDouble(txtPLength.Text);
                printer.GridSubAreaName =cmbSubArea.Text;
                printer.PWidth = Convert.ToDouble(txtPWidth.Text);
                printer.PDes = txtPDes.Text;
                printer.ICCPath = @"\\";
                printer.hotDirPath = txthotDirPath.Text;
                printer.MonitorDirPath = txtMonitorDirPath.Text;
                printer.IsTransverse = chkIsTransverse.Checked;
                printer.IsMiddlePrint = checkEditIsMiddle.Checked;
                printer.IsAutoCut = chkAutoCut.Checked;
                printer.status = 0;
                printer.status2 = 0;
                switch(cmbPrinterType.Text)
                {
                    case "宝丽,泰莱":
                        printer.PType = "1";
                    break;
                    case "Frontier":
                        printer.PType = "2";
                    break;
                    case "文印":
                        printer.PType = "3";
                    break;
                    case "喷绘":
                        printer.PType = "4";
                    break;
                }
                printer.status3 = true;
                printer.speed = 0;
                printer.MaxHeight = Convert.ToDecimal(textEditMaxHeigth.Text);
                printer.MinHeight = Convert.ToDecimal(textEditMinHeigth.Text);
                printer.MaxWidth = Convert.ToDecimal(textEditMaxWidth.Text);
                printer.MinWidth = Convert.ToDecimal(textEditMinWidth.Text);
                printer.CutFlagFrontOrBehind = comboBoxCutFlagFrontOrBehind.SelectedIndex;
                _controller.AddPrinter(printer, cmbPaperFace.Text, cmbPaperType.Text,0);

                //link datagrid
                _controller.Grid = gridControlPrinter;
                _controller.View = gridViewPrinter;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // Update Shop

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtPName.Focus();
                return;
            }
            if (CheckData())
            {

                _printer.PName = txtPName.Text;
                _printer.PDPI =Convert.ToInt32(txtPDPI.Text);
                _printer.PLength = Convert.ToDouble(txtPLength.Text);
                _printer.GridSubAreaName = cmbSubArea.Text;
                _printer.PWidth = Convert.ToDouble(txtPWidth.Text);
                _printer.PDes = txtPDes.Text;
                _printer.ICCPath = txtICCPath.Text;
                _printer.hotDirPath = txthotDirPath.Text;
                _printer.MonitorDirPath = txtMonitorDirPath.Text;
                switch (cmbPrinterType.Text)
                {
                    case "宝丽,泰莱":
                        _printer.PType = "1";
                        break;
                    case "Frontier":
                        _printer.PType = "2";
                        break;
                    case "文印":
                        _printer.PType = "3";
                        break;
                    case "喷绘":
                        _printer.PType = "4";
                        break;
                }
                _printer.IsTransverse = chkIsTransverse.Checked;
                _printer.IsMiddlePrint = checkEditIsMiddle.Checked;
                _printer.IsAutoCut = chkAutoCut.Checked;
                _printer.MaxHeight = Convert.ToDecimal(textEditMaxHeigth.Text);
                _printer.MinHeight = Convert.ToDecimal(textEditMinHeigth.Text);
                _printer.MaxWidth = Convert.ToDecimal(textEditMaxWidth.Text);
                _printer.MinWidth = Convert.ToDecimal(textEditMinWidth.Text);
                _printer.CutFlagFrontOrBehind = comboBoxCutFlagFrontOrBehind.SelectedIndex;
                _controller.UpdatePrinter(_printer,cmbPaperFace.Text, cmbPaperType.Text, 0);

                //link datagrid
                _controller.Grid = gridControlPrinter;
                _controller.View = gridViewPrinter;
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // Delete shop
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (_printer != null)
            {
                if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {

                    _controller.DeletePrinter(_printer);

                    //link datagrid
                    _controller.Grid = gridControlPrinter;
                    _controller.View = gridViewPrinter;
                    _controller.GetDate();
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        // Cancel button
        private void btnCancel_Click(object sender, EventArgs e)
        {
            cmbPrinterType.Enabled = false;
            cmbPaperFace.Enabled = false;
            cmbPaperType.Enabled = false;
            txtPName.Enabled = false;
            txtPDPI.Enabled = false;
            txtPLength.Enabled = false;
            cmbSubArea.Enabled = false;
            txtPWidth.Enabled = false;
            txtPDes.Enabled = false;
            txtICCPath.Enabled = false;
            txthotDirPath.Enabled = false;
            txtMonitorDirPath.Enabled = false;
            chkIsTransverse.Enabled = false;
            checkEditIsMiddle.Enabled = false;
            chkAutoCut.Enabled = false;
            textEditMinWidth.Enabled = false;
            textEditMaxWidth.Enabled = false;
            textEditMinHeigth.Enabled = false;
            textEditMaxHeigth.Enabled = false;
            comboBoxCutFlagFrontOrBehind.Enabled = false;
            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }
        // Reset printer
        private void btnReset_Click(object sender, EventArgs e)
        {
            if (_printer != null)
            {
                _printer.status = 0;
                _printer.status2 = 0;
                _controller.UpdatePrinter(_printer, cmbPaperFace.Text, cmbPaperType.Text,0);

                //link datagrid
                _controller.Grid = gridControlPrinter;
                _controller.View = gridViewPrinter;
                _controller.GetDate();
                MessageBox.Show("打印机复位成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("请选择要复位的打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        private void gridViewPrinter_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _printer = (PrinterInfo)gridViewPrinter.GetRow(e.FocusedRowHandle);
            if (_printer != null)
            {
                txtPName.Text = _printer.PName;
                txtPDPI.Text = _printer.PDPI.ToString();
                txtPLength.Text = _printer.PLength.ToString();

                cmbPaperFace.Text = _printer.PaperFaceName;
                cmbPaperType.Text = _printer.PaperTypeName;

                switch(_printer.PType)
                {
                    case "1":
                        cmbPrinterType.Text ="宝丽,泰莱";
                    break;
                    case "2":
                    cmbPrinterType.Text = "Frontier";
                    break;
                    case "3":
                    cmbPrinterType.Text = "文印";
                    break;
                    case "4":
                    cmbPrinterType.Text = "喷绘";
                    break;
                }

                cmbSubArea.Text = _printer.GridSubAreaName;
                txtPWidth.Text = _printer.PWidth.ToString();
                txtPDes.Text = _printer.PDes;
                txtICCPath.Text = _printer.ICCPath;
                txthotDirPath.Text = _printer.hotDirPath;
                txtMonitorDirPath.Text = _printer.MonitorDirPath;
                textEditMaxHeigth.Text = _printer.MaxHeight.ToString();
                textEditMaxWidth.Text = _printer.MaxWidth.ToString();
                textEditMinHeigth.Text = _printer.MinHeight.ToString();
                textEditMinWidth.Text = _printer.MinWidth.ToString();
                chkIsTransverse.Checked = (bool)_printer.IsTransverse;
                checkEditIsMiddle.Checked = (bool)_printer.IsMiddlePrint;
                chkAutoCut.Checked = _printer.IsAutoCut;
                txtPrinterPaperBoxNum.Text = _controller.GetPaperBox(_printer.PrinterID);
                comboBoxCutFlagFrontOrBehind.SelectedIndex = _printer.CutFlagFrontOrBehind;
            }
        }

        /// <summary>
        /// 检查数据完整性

        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtPName.Text == string.Empty)
            {
                txtPName.Focus();
                MessageBox.Show("打印机名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtPDPI.Text == string.Empty)
            {
                txtPDPI.Focus();
                MessageBox.Show("打印机分辨率不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            //if (cmbPaperFace.Text == string.Empty)
            //{
            //    cmbPaperFace.Focus();
            //    MessageBox.Show("纸面不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;
            //}
            //if (cmbPaperType.Text == string.Empty)
            //{
            //    cmbPaperType.Focus();
            //    MessageBox.Show("纸型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;
            //}
            if (txtPLength.Text == string.Empty)
            {
                txtPLength.Focus();
                MessageBox.Show("打印纸长度不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (cmbSubArea.Text == string.Empty)
            {
                cmbSubArea.Focus();
                MessageBox.Show("打印机分区不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtPWidth.Text == string.Empty)
            {
                txtPWidth.Focus();
                MessageBox.Show("打印纸宽度不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txthotDirPath.Text == string.Empty)
            {
                txthotDirPath.Focus();
                MessageBox.Show("热文件夹路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtMonitorDirPath.Text == string.Empty)
            {
                txtMonitorDirPath.Focus();
                MessageBox.Show("监控文件夹路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            //if (txtICCPath.Text == string.Empty)
            //{
            //    txtICCPath.Focus();
            //    MessageBox.Show("ICC文件存放路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;
            //}
            if(string.IsNullOrEmpty(textEditMaxHeigth.Text.Trim())||string.IsNullOrEmpty(textEditMaxWidth.Text.Trim())||string.IsNullOrEmpty(textEditMinHeigth.Text.Trim())||string.IsNullOrEmpty(textEditMinWidth.Text.Trim()))
            {
                textEditMaxHeigth.Focus();
                MessageBox.Show("打印机打印范围设置不正确", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            cmbPrinterType.Enabled = bl;
            cmbPaperFace.Enabled = bl;
            cmbPaperType.Enabled = bl;
            txtPName.Enabled = bl;
            txtPDPI.Enabled = bl;
            txtPLength.Enabled = bl;
            cmbSubArea.Enabled = bl;
            txtPWidth.Enabled = bl;
            txtPDes.Enabled = bl;
            txtICCPath.Enabled = bl;
            txthotDirPath.Enabled = bl;
            txtMonitorDirPath.Enabled = bl;
            textEditMinWidth.Enabled = bl;
            textEditMaxWidth.Enabled = bl;
            textEditMinHeigth.Enabled = bl;
            textEditMaxHeigth.Enabled = bl;
            chkIsTransverse.Enabled = bl;
            chkAutoCut.Enabled = bl;
            checkEditIsMiddle.Enabled = bl;
            comboBoxCutFlagFrontOrBehind.Enabled = bl;

            btnhotDirPath.Enabled = bl;
            btnMonitorDirPath.Enabled = bl;
            btnICCPath.Enabled = bl;

            cmbPaperFace.Text = string.Empty;
            cmbPaperType.Text = string.Empty;
            txtPName.Text = string.Empty;
            txtPDPI.Text = string.Empty;
            txtPLength.Text = string.Empty;
            cmbSubArea.Text = string.Empty;
            txtPWidth.Text = string.Empty;
            txtPDes.Text = string.Empty;
            txtICCPath.Text = string.Empty;
            txthotDirPath.Text = string.Empty;
            txtMonitorDirPath.Text = string.Empty;
            chkIsTransverse.Checked = false;
            chkAutoCut.Checked = false;
            checkEditIsMiddle.Enabled = true;


            if (bl)
            {

                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            cmbPrinterType.Enabled = bl;
            cmbPaperFace.Enabled = bl;
            cmbPaperType.Enabled = bl;
            txtPName.Enabled = bl;
            txtPDPI.Enabled = bl;
            txtPLength.Enabled = bl;
            cmbSubArea.Enabled = bl;
            txtPWidth.Enabled = bl;
            txtPDes.Enabled = bl;
            txtICCPath.Enabled = bl;
            txthotDirPath.Enabled = bl;
            txtMonitorDirPath.Enabled = bl;
            chkIsTransverse.Enabled = bl;
            chkAutoCut.Enabled = bl;
            checkEditIsMiddle.Enabled = bl;

            textEditMinWidth.Enabled = bl;
            textEditMaxWidth.Enabled = bl;
            textEditMinHeigth.Enabled = bl;
            textEditMaxHeigth.Enabled = bl;

            btnhotDirPath.Enabled = bl;
            btnMonitorDirPath.Enabled = bl;
            btnICCPath.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }
        //热文件夹路径选择
        private void btnhotDirPath_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog1.ShowDialog())
            {
                if (folderBrowserDialog1.SelectedPath.Trim().Substring(0, 2) == @"\\")
                {
                    txthotDirPath.Text = folderBrowserDialog1.SelectedPath;
                }
                else
                {
                    MessageBox.Show("请从网上邻居中选择网络路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txthotDirPath.Text = "";
                }
            }
        }
        //监控文件夹路径选择
        private void btnMonitorDirPath_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog2.ShowDialog())
            {
                if (folderBrowserDialog2.SelectedPath.Trim().Substring(0, 2) == @"\\")
                {
                    txtMonitorDirPath.Text = folderBrowserDialog2.SelectedPath;
                }
                else
                {
                    MessageBox.Show("请从网上邻居中选择网络路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtMonitorDirPath.Text = "";
                }
            }
        }
        //icc文件路径选择
        private void btnICCPath_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "ICC文件(*.ICC)|*.ICC|所有文件(*.*)|*.*";
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {

                if (openFileDialog1.FileName.Trim().Substring(0, 2) == @"\\")
                {
                    txtICCPath.Text = openFileDialog1.FileName;
                }
                else
                {
                    MessageBox.Show("请从网上邻居中选择网络路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtICCPath.Text = "";
                }
            }
        }

        private void FrmPrinterManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }
        
        private void txtPWidth_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void LinkPrinter()
        {
            _controller.Grid = gridControlPrinter;
            _controller.View = gridViewPrinter;
            _controller.GetDate();
        }
        private void LinkSubArea()
        {
            _controller.ComoBox = cmbSubArea;
            _controller.LinkSubArea();
            
        }
        private void LinkPaperFace()
        {
            _controller.ComoBox = cmbPaperFace;
            _controller.LinkPaperFace();
        }
        private void LinkPaperType()
        {
            _controller.ComoBox = cmbPaperType;
            _controller.LinkPaperType();
        }
        private void LinkPaperType1()
        {
            _controller.ComoBox = cmbpaperTypeIcc;
            _controller.LinkPaperType();
        }

        private void btnICCPath1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "ICC文件(*.ICC)|*.ICC|所有文件(*.*)|*.*";
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {

                if (openFileDialog1.FileName.Trim().Substring(0, 2) == @"\\")
                {
                    txtIccPaperType.Text = openFileDialog1.FileName;
                }
                else
                {
                    MessageBox.Show("请从网上邻居中选择网络路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtIccPaperType.Text = "";
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbpaperTypeIcc.Text))
            {
                MessageBox.Show("请选择纸型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtIccPaperType.Text))
            {
                MessageBox.Show("请选择ICC文件路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtSharping.Text))
            {
                MessageBox.Show("请输入锐化值!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            try
            {
                _controller.UpdatePaperTypeICC(_printer.PrinterID, cmbpaperTypeIcc.Text, txtIccPaperType.Text,Convert.ToInt32( txtSharping.Text));
                MessageBox.Show("设置成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void cmbpaperTypeIcc_Closed(object sender, ClosedEventArgs e)
        {
            try
            {
                txtIccPaperType.Text = _controller.GetIccPath(_printer.PrinterID,cmbpaperTypeIcc.Text).ICCPath;
                txtSharping.Text = _controller.GetIccPath(_printer.PrinterID, cmbpaperTypeIcc.Text).SharpVale.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnPrinterPaperBoxNumOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPrinterPaperBoxNum.Text.Trim()))
            {
                try
                {
                    _controller.AddPaperBox(_printer.PrinterID, Convert.ToInt32(txtPrinterPaperBoxNum.Text));
                    MessageBox.Show("纸箱数量设定成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                
            }
            else
            {
                MessageBox.Show("请输入纸箱数量", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

       
        
    }
}
